/*
 * @Author: fhw
 * @Date: 2022-07-04 11:27:14
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-01-31 11:51:04
 * @Description:
 */
import { ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import * as path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default ({ mode }: ConfigEnv) => {
	const server =
		mode === 'development'
			? {
					port: 8080,
					proxy: {
						'/serverApi': {
							target: 'http://127.0.0.1:3002',
							changeOrigin: true,
							rewrite: (path: string) => path.replace(/^\/serverApi/, ''),
						},
					},
					headers: {
						'Access-Control-Allow-Origin': '*',
					},
					disableHostCheck: true,
					host: '0.0.0.0',
					cors: true,
			  }
			: {}
	return {
		plugins: [
			AutoImport({
				resolvers: [ElementPlusResolver()],
			}),
			Components({
				resolvers: [ElementPlusResolver()],
			}),
			vue(),
			vueJsx(),
		],
		base: '/',
		root: './', // index.html 文件所在的位置
		envDir: './env', // 加载 .env 文件的目录
		css: {
			modules: {
				scopeBehaviour: 'local',
			},
			preprocessorOptions: {
				less: {
					javascriptEnabled: true,
				},
				scss: {
					// 引入scss全局变量 给导入的路径最后加上;
					// additionalData: `@import "./src/style/app.scss";@import "./src/style/variables.scss";`,
					javascriptEnabled: true,
				},
			},
		},
		resolve: {
			alias: {
				'@': path.resolve(__dirname, './src'),
			},
			extensions: ['.js', '.vue', '.json', '.ts', '.tsx', '.gz'], // 加入ts 和 tsx
		},
		server,
		build: {
			target: 'modules', // modules es6 es2015  esnext
			minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
			manifest: false, // 是否产出maifest.json
			sourcemap: mode === 'beta', // 是否产出soucemap.json
			outDir: './dist', // 产出目录
			//	rollupOptions
		},
		optimizeDeps: {
			include: ['vue', 'vue-router'],
		},
	}
}
